package supermercadoSVN;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class Repository {

	public void agregarProducto (Producto producto) throws SQLException{
		
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/agenda?user=root");
		
		String sql = "INSERT INTO producto (codigo, desc, precio) VALUES (?,?,?)";
		
		PreparedStatement pSt = connection.prepareStatement(sql);
		
		pSt.setInt (1,producto.getCodigo());
		pSt.setString(2, producto.getDesc());
		pSt.setDouble(3, producto.getPrecio());
	
		pSt.execute();
	}
	public void agregarFactura (Factura factura) throws SQLException{
		

		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/agenda?user=root");
		String sql = "INSERT INTO factura (fecha, total) VALUES (?,?)";
		PreparedStatement pSt = connection.prepareStatement(sql);
		
		pSt.setDate(1,new java.sql.Date(factura.getFecha().getTime()));
		//pSt.setDouble(2, factura.calcularTotal());
	
		pSt.execute();

		ResultSet rs = pSt.getGeneratedKeys();
		rs.next();
		int id = rs.getInt(1);
		rs.close();
		
		for (Compra compra : factura.getListaCompras()){
			
		}
		
	
	
	
	
	}
	
	public void agregarCompra(Compra compra, Factura factura) throws SQLException{
		
		Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/agenda?user=root");
		String sql = "INSERT INTO compra (id_fact, cantidad, id_producto ) VALUES (?,?,?)";
		
		PreparedStatement pSt = connection.prepareStatement(sql);
		pSt.setInt(1,factura.getIdFact());
		
	}
	
	
	
}
